//Evento agregado para Ares Server Lineage II
//By AsuS
//www.ares-games.net
package com.l2jserver.gameserver.model.actor.instance;

import javolution.text.TextBuilder;

import com.l2jserver.Config;
import com.l2jserver.gameserver.Announcements;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.entity.HideAndSeek;
import com.l2jserver.gameserver.model.entity.TvTEvent;
import com.l2jserver.gameserver.model.olympiad.Olympiad;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;

/**
 * @author BiggBoss
 */
public class L2HaSEventManagerInstance extends L2Npc {

	private boolean _isHided;
	
	public L2HaSEventManagerInstance(int objectId, L2NpcTemplate template) {
		super(objectId, template);
	}
	
	@Override
	public void showChatWindow(L2PcInstance player, int val) {
		NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
		
		if(_isHided) {
			msg.setHtml(hideWindow(player));
		}
		else {
			msg.setHtml(regWindow(player));
		}
		
		msg.replace("%objectId%", String.valueOf(this.getObjectId()));
		player.sendPacket(msg);
	}
		
	public void hideMe(boolean b) {
		_isHided = b;
	}
	
	@Override
	public void onBypassFeedback(L2PcInstance player, String command) {
		
		if(player == null) return;
		
		if(command.startsWith("joinHas")) {
			if(!HideAndSeek.isRegistering()) {
				player.sendMessage("You cannot join at this time!");
				return;
			}
			if(Olympiad.getInstance().isRegistered(player)) {
				player.sendMessage("You can't join while in olympiad!");
				return;
			}
			else if(TvTEvent.isPlayerParticipant(player.getObjectId())) {
				player.sendMessage("You can't join while in TvT Event");
				return;
			}
			else if(player.getKarma() > 0 || player.isCursedWeaponEquipped()) {
				player.sendMessage("You can't join while you have karma!");
				return;
			}
			else if(HideAndSeek.addPlayer(player)) {
				player.sendMessage("Registered for Hide And Seek");
			}
			else return;
		}
		else if(command.startsWith("leaveHas")) {
			if(HideAndSeek.isRegistering()) {
				if(HideAndSeek.removePlayer(player))
					player.sendMessage("Removed from event waiting list!");
				else {
					player.sendMessage("You are not participating!");
					return;
				}
			}
			else {
				player.sendMessage("You can't unregister yourself at this time");
				return;
			}
		}
		else if(command.startsWith("foundHas")) {
			if(player.isInHASEvent()) {
				HideAndSeek.setWinner(true);
				HideAndSeek.rewardWinner(player);
				Announcements.getInstance().announceToAll(player.getName()+" won the Hide & Seek event!");
			}
		}
	}
	
	private String hideWindow(L2PcInstance player) {
		TextBuilder tb = new TextBuilder();
		tb.append("<html><title>Hide & Seek AreS Event</title><body><br><center>");
		if(player.isInHASEvent()) {
			tb.append("Shhhh...seems like you are the only<br1>");
			tb.append("who foumd me!. Are you going to tell<br1>");
			tb.append("everybody where i am?<br>");
			tb.append("<button value=\"Finish\" action=\"bypass -h npc_%objectId%_foundHas\" width=176 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
		}
		else {
			tb.append("Yes, you found me, but you are<br1>");
			tb.append("not participating so...Are you sure<br1>");
			tb.append("you want to ruin the event?");
			if(Config.HAS_SHUTNONMENBERS) {
				byte minutes = (byte) ((HideAndSeek._timeLeft / 1000 ) / 60);
				player.setPunishLevel(L2PcInstance.PunishLevel.CHAT, minutes);
				tb.append("To be sure, i will close you mouth<br1>");
				tb.append("till the event ends =)!");
			}
		}
		tb.append("</center></body></html>");
		return tb.toString();
	}
	
	private String regWindow(L2PcInstance player) {
		TextBuilder tb = new TextBuilder();
		tb.append("<html><title>Hide & Seek AreS Event</title><body><br><center>");
		if(HideAndSeek.isRegistering()) {
			if(HideAndSeek._playersInEvent.contains(player)) {
				tb.append("You are alredy register...wait a<br1>");
				tb.append("moment till it starts.<br>");
				tb.append("You maybe prefer to remove your<br1>");
				tb.append("participation?<br>");
				tb.append("<button value=\"Leave Event\" action=\"bypass -h npc_%objectId%_leaveHas\" width=176 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
			}
			else {
				tb.append("Hello! Would you like to join the<br1>");
				tb.append("Hide and Seek event? Just click<br1>");
				tb.append("the 'Register' button<br>");
				tb.append("<button value=\"Register\" action=\"bypass -h npc_%objectId%_joinHas\" width=176 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
			}
		}
		else {
			tb.append("Fast! Go find the hided npc!<br1>");
			tb.append("Im not going to register/unregister<br1>");
			tb.append("you at this moment!");
			
		}
		tb.append("</center></body></html>");

		return tb.toString();
	}
}
